﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.Connect;
using Kooboo.IoC;

using System.Data;
using Kooboo.Domain;

namespace Kooboo.Commerce
{
    public static class UserAction
    {
        public static ICustomer GetCustomer(this User user)
        {
            return ContextContainer.Current.Resolve<ICustomerRepository>()
                .Customers
                .Where(i => i.UserName == user.Name)
                .FirstOrDefault();
        }

        public static IQueryable<IOrder> Orders(this User user)
        {
            var customer = user.GetCustomer();

            if (customer == null)
            {
                return Enumerable.Empty<IOrder>().AsQueryable();
            }
            else
            {
                return ContextContainer.Current.Resolve<IOrderRepository>()
                    .Orders
                    .Where(i => i.Customer.Id == customer.Id);
            }
        }

        public static IOrder Order(this User user, Int32 orderId)
        {
            var customer = user.GetCustomer();
            if (customer == null)
            {
                return null;
            }
            else
            {
                return ContextContainer.Current.Resolve<IOrderRepository>()
                    .Orders
                    .Where(i => i.Customer.Id == customer.Id && i.Id == orderId).FirstOrDefault();
            }
        }

        public static User Live(this User user)
        {
            var findUser = UserServices.FindUser(user.Name);

            if (findUser == null)
            {
                var createStatus = UserServices.CreateUser(user.Name, user.Membership.Password, user.Email);
                if (createStatus == UserCreateStatus.Success)
                {
                    return user;
                }
                else
                {
                    throw new Exception("Can NOT create the account.");
                }
            }
            else
            {
                var validatedUser = UserServices.ValidateUser(user.Name, user.Membership.Password);
                if (validatedUser == null)
                {
                    throw new Exception("The password is incorrect.");
                }
                else
                {
                    return validatedUser;
                    
                }
            }

        }

        public static void ToCustomer(this User user,string password)
        {
            var findUser = user.Live();

            var service = ContextContainer.Current
                .Resolve<ICustomerRepository>();
            var isCustomer = service.Customers.Any(i => i.UserName == user.Name);

            if (isCustomer == false)
            {
                var customer = ObjectContainer.CreateInstance<ICustomer>();
                customer.UserName = user.Name;
                customer.CreateDate = DateTime.Now;

                using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
                {
                    try
                    {
                        if (string.IsNullOrWhiteSpace(customer.UserName))
                        {
                            throw new ArgumentNullException("Customer.UserName");
                        }

                        if (service.Customers.Any(i => i.UserName == customer.UserName))
                        {
                            throw new DuplicateNameException(customer.UserName);
                        }

                        var find = Kooboo.Connect.UserServices.FindUser(customer.UserName);
                        if (find == null)
                        {
                            var status = Kooboo.Connect.UserServices.CreateUser(customer.UserName, password, customer.MailAddress);

                            if (status != Connect.UserCreateStatus.Success)
                            {
                                throw new InvalidOperationException(status.ToString());
                            }

                        }
                        customer.CreateDate = DateTime.Now;
                        service.Post(customer);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
           
        }
    }
}
